import Ember from 'ember';
import BaseBusiness from '../base-business';

export default BaseBusiness.extend({
  queryParams: {
      id: {
          refreshModel: true
      },
      editMode: {
          refreshModel: true
      },
  },
  header_title:'护理方案信息',
  model(){
    return{};
  },

  setupController(controller,model){
    var _self = this;
    this._super(controller, model);
    var editMode=this.getCurrentController().get('editMode');
    var id=this.getCurrentController().get('id');
    let serviceArray = new Ember.A();
    if(editMode=='edit'){
      this.store.findRecord('nursingproject',id).then(function(projectInfo){
        controller.set('projectInfo',projectInfo);
        let questionnaireList = _self.store.query('evaluateresult',{filter:{customer:{id:projectInfo.get('customer.id')}}}).then(function(questionnaireList){
          controller.set('questionnaireList',questionnaireList);
          //取用户的最近一份问卷
          let recentResult = questionnaireList.get('lastObject');
          console.log('recentResult:',recentResult.get('id'));
          controller.set('resultID',recentResult.get('id'));
          //如果问卷按分数评测
          if(recentResult.get('score')){
            let score = recentResult.get('score');
            //根据分数，取相应分数区间的护理等级列表（分数过滤有问题）
            _self.store.query('nursinglevel',{filter:{'lowScore@$lte':score,'highScore@$gte':score}}).then(function(nursinglevel){
              let curLevel = nursinglevel.get('firstObject');
              let services = curLevel.get('services').then(function(services){
                services.forEach(function(service){
                  console.log(service);
                  console.log('service name:',service.get('item.name'));
                  //将护理项目放入推荐项目数组中
                  serviceArray.pushObject(service.get('item'));
                  controller.incrementProperty('flag');
                });
              });
              console.log("services:",curLevel.get('services'));

            });
          }else{
            //如果问卷按照项目评测
            _self.store.query('evaluateexameranswer',{filter:{"[result][id]":recentResult.get('id')}}).then(function(answers){
              answers.forEach(function(answer) {
                  //查询护理项目
                  answer.get("answer").then(function(answers){
                    answers.get("services").then(function(services){
                      var len = services.get("length");
                      for(var i=0;i<len;i++){
                        var service = services.objectAt(i);
                        if (!serviceArray.findBy("id", service.get("id"))) {
                            serviceArray.pushObject(service);
                              controller.incrementProperty("flag");
                        }
                      }
                    });
                  });
              });
            });
          }
        });
      });
    }else{
      //增加新的护理方案
      controller.set('detailEdit',true);
      this.store.findRecord('nursingproject',id).then(function(projectInfo){
        controller.set('projectInfo',projectInfo);
        //获取用户问卷列表
        let questionnaireList = _self.store.query('evaluateresult',{filter:{customer:{id:projectInfo.get('customer.id')}}}).then(function(questionnaireList){
          controller.set('questionnaireList',questionnaireList);
          //取用户的最近一份问卷
          let recentResult = questionnaireList.get('lastObject');
          console.log('recentResult:',recentResult.get('id'));
          //如果问卷按分数评测
          if(recentResult.get('score')){
            let score = recentResult.get('score');
            //根据分数，取相应分数区间的护理等级列表（分数过滤有问题）
            _self.store.query('nursinglevel',{filter:{'lowScore@$lte':score,'highScore@$gte':score}}).then(function(nursinglevel){
              let curLevel = nursinglevel.get('firstObject');
              let services = curLevel.get('services').then(function(services){
                services.forEach(function(service){
                  console.log(service);
                  console.log('service name:',service.get('item.name'));
                  //将护理项目放入推荐项目数组中
                  serviceArray.pushObject(service.get('item'));
                  controller.incrementProperty('flag');
                });
              });
              console.log("services:",curLevel.get('services'));

            });
          }else{
            //如果问卷按照项目评测
            _self.store.query('evaluateexameranswer',{filter:{"[result][id]":recentResult.get('id')}}).then(function(answers){
              answers.forEach(function(answer) {
                  //查询护理项目
                  answer.get("answer").then(function(answers){
                    answers.get("services").then(function(services){
                      var len = services.get("length");
                      for(var i=0;i<len;i++){
                        var service = services.objectAt(i);
                        if (!serviceArray.findBy("id", service.get("id"))) {
                            serviceArray.pushObject(service);
                              controller.incrementProperty("flag");
                        }
                      }
                    });
                  });
              });
            });
          }
        });
      });
      controller.set('resultServiceList',serviceArray);
    }
    var serviceList=this.store.query('customerserviceitem',{}).then(function(services){
      _self.getCurrentController().set("serviceList", services);
    });
  }
});
